package com.demo.easy.manySheet;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author:YuXuePeng
 * @Date:Created in 2023/8/14 15:41
 */
public class DemoManySheetsUtil {
    public static ByteArrayOutputStream outPutStreamByModel(Map<String, Object> dataMap, List dataList, InputStream templateIs, int[] mergeColumnIndex, Integer mergeRowIndex) throws Exception {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ExcelWriter excelWriter = EasyExcel.write(outputStream).inMemory(Boolean.TRUE).withTemplate(templateIs).excelType(ExcelTypeEnum.XLS).build();
        WriteSheet writeSheet = null;
//        if(mergeColumnIndex != null && mergeRowIndex != null) {
//            int mergeRowIndexInt = mergeRowIndex.intValue();
//            writeSheet = EasyExcel.writerSheet().registerWriteHandler(new ExcelFillCellMergeStrategy(mergeRowIndexInt, mergeColumnIndex)).build();
//        } else {
//            writeSheet = EasyExcel.writerSheet().build();
//        }
        writeSheet = EasyExcel.writerSheet().build();
        FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
        if(dataList == null) {
            dataList = new ArrayList();
        }
        excelWriter.fill(dataList, fillConfig, writeSheet);

        if(dataMap != null && !dataMap.isEmpty()) {
            excelWriter.fill(dataMap, writeSheet);
        }

        excelWriter.finish();
        return outputStream;
    }

    public static ByteArrayOutputStream outPutStreamByModelDemo(Map<String, Object> map,InputStream templateIs){
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ExcelWriter excelWriter = EasyExcel.write(outputStream)
                .inMemory(Boolean.TRUE)
                .withTemplate(templateIs)
                .excelType(ExcelTypeEnum.XLS)
                .build();
        WriteSheet writeSheet = null;
        WriteSheet writeSheet1 = null;
        writeSheet = EasyExcel.writerSheet("页码1").build();
        writeSheet1 = EasyExcel.writerSheet("页码2").build();
        FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();

        excelWriter.fill(map.get("one"), fillConfig,writeSheet);
        excelWriter.fill(map.get("two"), fillConfig,writeSheet1);
        System.out.println(writeSheet.getSheetName());
        excelWriter.finish();
        return outputStream;
    }
}
